package com.unicomcz.article;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import com.unicomcz.ActionBase;


public class ViewPopArticleAction extends ActionBase {
	private String artId;
    private String artTitle;
    private String artContent;
    private String operTime;
    private String loginType;
	public String getArtId() {
		return artId;
	}
	public void setArtId(String artId) {
		this.artId = artId;
	}
	public String getArtTitle() {
		return artTitle;
	}
	public void setArtTitle(String artTitle) {
		this.artTitle = artTitle;
	}
	public String getArtContent() {
		return artContent;
	}
	public void setArtContent(String artContent) {
		this.artContent = artContent;
	}
	public String getOperTime() {
		return operTime;
	}
	public void setOperTime(String operTime) {
		this.operTime = operTime;
	}
	public String getLoginType() {
		return loginType;
	}
	public void setLoginType(String loginType) {
		this.loginType = loginType;
	}
	public String execute() throws Exception{
		log = Logger.getLogger(this.getClass());
		
		if ((loginType!=null)&&(loginType.compareTo("login")!=0)){
           if (!validatePriv()) {
               //校验登陆错误
               return "login";
           }
		}
		//首先取出可用的数据库
		String sqlStr="SELECT art_title,to_char(oper_time,'yyyy-mm-dd'),art_id" + 
                " FROM dls_article WHERE status=0 and art_type='"+loginType+"' ORDER BY oper_time DESC";
		//PreparedStatement pstmt = null;
		ResultSet rs = null;
        try{
        	boolean dbFlag = setupConn("dls");
        	
            if (!dbFlag) {
                log.debug(userInfo.getUserId()+"登陆系统失败!因为建立数据库连接失败");
                return "fail";
            }
            pstmt = conn.prepareStatement(sqlStr);
            rs = pstmt.executeQuery();
            if (rs.next()){
            	String tmpArtId = rs.getString(3);
            	log.debug("阅读文章:"+tmpArtId);
            	rs.close();
            	pstmt.close();
            	sqlStr = "SELECT art_title,to_char(oper_time,'yyyy-mm-dd'),art_Content " + 
             		   " FROM dls_article where art_id=? and status=0";
            	pstmt = conn.prepareStatement(sqlStr);
            	pstmt.setString(1, tmpArtId);
            	rs = pstmt.executeQuery();
            	if (!rs.next()){
            		addActionMessage("文章不存在！");
                    return "fail";
            	}else{
            		artTitle=rs.getString(1);
                    operTime=rs.getString(2);
                    artContent = rs.getString(3);
            	}
            	
            }else{
            	addActionMessage("文章不存在！");
                return "fail";
            }
        }catch(Exception e){
        	log.debug(e);
        	addActionMessage("获取文章失败！");
            return "fail";
        }finally{
        	try {
                rs.close();
            }catch(Exception e) {
                
            }
        	tearDown();
        }
        return SUCCESS;
    }
	

}
